package cc.fs.zhihuapp.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cc.fs.zhihuapp.R;

/**
 * Created by fostion on 2015/8/29.
 */
public class EmptyView extends RelativeLayout{

    public static final int HIDEN_LAYOUT = 0;
    public static final int ERROR_NETWORK = 1;
    public static final int NO_DATA = 2;

    private int mErrorState = 0;
    private View mView;
    private View clickView;
    private ImageView image;
    private TextView text;
    private OnRetryClick mOnRetryClick = null;

    public EmptyView(Context context) {
        super(context);
        init();
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EmptyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mView = LayoutInflater.from(getContext()).inflate( R.layout.layout_error,null);
        clickView = (RelativeLayout)mView.findViewById(R.id.clickView);
        image = (ImageView)mView.findViewById(R.id.errorImg);
        text = (TextView)mView.findViewById(R.id.errorTV);
        setErrorState(HIDEN_LAYOUT);
        addView(mView);
    }

    public void setErrorState(int error){
        mErrorState = error;
        switch (mErrorState){
            case HIDEN_LAYOUT:
                setVisibility(View.GONE);
                break;
            case ERROR_NETWORK:
                image.setImageResource(R.mipmap.page_icon_network);
                text.setText("网络异常，点击重试!");
                setVisibility(View.VISIBLE);
                break;
            case NO_DATA:
                image.setImageResource(R.mipmap.page_icon_empty);
                text.setText("暂无内容");
                setVisibility(View.VISIBLE);
                break;
        }
    }

    public void setOnRetryClick(OnRetryClick onRetryClick){
        this.mOnRetryClick = onRetryClick;
        clickView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnRetryClick != null){
                    mOnRetryClick.retryClick();
                }
            }
        });
    }


   public interface OnRetryClick{
        void retryClick();
    }

}
